package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.ActivityDao;
import com.example.communist_app_api.db.dao.UserActivityDao;
import com.example.communist_app_api.db.dto.ActivityUniAppPersonalDTO;
<<<<<<< HEAD
import com.example.communist_app_api.db.pojo.UserActivity;
<<<<<<< HEAD
import com.example.communist_app_api.enums.SenderNameEnum;
=======
import com.example.communist_app_api.db.pojoDto.UserActivity;
>>>>>>> 党建uniapp/dev
=======
>>>>>>> parent of aec1d46 (消息模块的搭建和配合其他模块消息的发送和收取)
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.UserActivityService;
import com.example.communist_app_api.vo.uniapp.resp.UniAppMyActivityRecordResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/26 18:17
 */
@Service
@Transactional
public class UserActivityServiceImpl implements UserActivityService {

    @Autowired
    private UserActivityDao userActivityDao;

    @Autowired
    private ActivityDao activityDao;

    @Override
    public HashMap<Integer, Integer> getApplicationNumberByActivityIdList(List<Integer> activityIdList) {
        HashMap<Integer, Integer> activityAttendMap = new HashMap<>();
        activityIdList.stream().forEach(n->{
            activityAttendMap.put(n,userActivityDao.getAttendActivityNum(n));
        });
        return activityAttendMap;
    }

    @Override
    public List<Integer> getUserIdListByActivityId(Integer activityId) {
        return userActivityDao.getUserIdListByActivityId(activityId);
    }

    @Override
    public void insertByUserIdAndActivityId(int userId, Integer activityId) {
        //TODO 判断是否超过活动限制人数，若超过限制人数就提示报满(超卖情况！！！)
        Integer limitedNum = activityDao.getLimitedNumByActivityId(activityId);
        Integer attendActivityNum = userActivityDao.getAttendActivityNum(activityId);
        if (attendActivityNum < limitedNum) {
            userActivityDao.insertByUserIdAndActivityId(userId,activityId);
        }else if (attendActivityNum >= limitedNum){
            throw new GlobalException("活动人数已满");
        }
    }

    @Override
    public Integer getApplicationNumberByActivityId(Integer activityId) {
        return userActivityDao.getAttendActivityNum(activityId);
    }

    @Override
    public Boolean getExistenceByUserIdAndActivityId(int userId, Integer activityId) {
        Integer existence = userActivityDao.getExistenceByUserIdAndActivityId(userId,activityId);
        if (existence != null) {
            //已经报满
            return true;
        }
        //未报名
        return false;
    }

    @Override
    public UniAppMyActivityRecordResp getAllActivityByUserId(String title,Integer userId) {
        //查询报名记录
        Integer signUpNumber = userActivityDao.getCountByUserId(userId);
        List<ActivityUniAppPersonalDTO> activityUniAppPersonalDTOList = userActivityDao.getActivityRecordByTitleAndUserId(title,userId);
        List<UserActivity> userActivityDaoSignUpListByUserId = userActivityDao.getSignUpListByUserId(userId);
        HashMap<Integer, Integer> signUpMap = new HashMap<>();
        userActivityDaoSignUpListByUserId.forEach(n->{
            signUpMap.put(n.getActivityId(),n.getSignIn());
        });
        //目前系统时间
        Date date = new Date(System.currentTimeMillis());

        activityUniAppPersonalDTOList.forEach(n->{
            //已签到并且系统时间超过活动结束时间为已完成
            if (signUpMap.get(n.getId()).equals(1) && date.compareTo(n.getEndTime())>0) {
                n.setStatus("已完成");
            }else if (signUpMap.get(n.getId()).equals(0) && date.compareTo(n.getStartTime())<0){
                //已报名且活动还未开始
                n.setStatus("已报名");
            }else if (signUpMap.get(n.getId()).equals(1) && date.compareTo(n.getStartTime())>0 &&date.compareTo(n.getEndTime())<0){
                n.setStatus("已签到");
            }else {
                n.setStatus("未签到");
            }
        });
        //实例化对象
        UniAppMyActivityRecordResp resp = new UniAppMyActivityRecordResp();
        resp.setSignUpNumber(signUpNumber);
        resp.setActivityUniAppPersonalDTOList(activityUniAppPersonalDTOList);
        return resp;
    }

    @Override
    public void cancelActivitySignUp(Integer activityId, Integer userId) {
        userActivityDao.cancelActivitySignUp(activityId,userId);
    }
}
